package com.wyf.android.advance.test

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.wyf.android.advance.R
import com.wyf.android.baselib.util.inflate

class Test1Adapter : BaseAdapter() {
    private var mList: ArrayList<TestViewData>? = null
    override fun getCount(): Int {
        return mList?.size ?: 0
    }

    override fun getItem(position: Int): TestViewData {
        return mList?.get(position) ?: TestViewData("", 0, SimpleData("", ""))
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var itemView = convertView ?: parent.inflate(R.layout.test_item)
        (itemView as TextView).text = getItem(position).toString()
        return itemView
    }

    fun setData(list: ArrayList<TestViewData>) {
        mList = list
        notifyDataSetChanged()
    }

    fun updateData(list: List<TestViewData>) {
        if (mList == null) {
            mList = ArrayList<TestViewData>()
        } else {
            mList!!.clear()
        }
        mList?.addAll(list)
        notifyDataSetChanged()
    }

}